<?php
namespace app\api\controller\order;

use app\api\controller\Api;
use app\common\audit\logic\AuditResultLogic;
use app\common\order\model\Order;
use app\common\order\model\OrderAdditional;
use app\common\system\model\OperateLog;
use Exception;
use think\db\exception\DbException;

class Additional extends Api {

    public function create() {
        $post = $this->request->post();
        $user = $this->request->user;

        if (empty($post['order_id'])) return error([], "参数错误");
        if (empty($post['company_id'])) return error([], "请选择承办公司");
        if (empty($post['additional'])) return error([], "参数错误");

        // 获取订单信息
        $orderModel = new Order();
        $order = $orderModel->where('id', $post['order_id'])->find();
        if (empty($order)) return error([], "订单不存在");

        $model = new OrderAdditional();
        $model->startTrans();

        $source = [];
        $modelName = OrderAdditional::class;
        $additional = $post['additional'];
        foreach ($additional as $row) {
            try {
                $row['order_id'] = $post['order_id'];
                $row['user_id'] = $user['id'];
                $additionalModel = new OrderAdditional();
                $additionalModel->save($row);
                $source[$modelName][] = $additionalModel->id;
            } catch (DbException $e) {
                $model->rollback();
                return error([], $e->getMessage());
            }
        }

        $auditRsLogic = new AuditResultLogic();
        $master = $order->getData();
        $options = [
            'company_id' => $post['company_id'],
            'timing' => 'additional'
        ];
        try {
            $auditNo = $auditRsLogic->initAudit($master, $user, $source, $options);
            foreach ($source as $className=>$row) {
                try {
                    $where = [];
                    if (is_array($row)) {
                        $where[] = ['id', 'in', $row];
                    } else {
                        $where[] = ['id', '=', $row];
                    }
                    $class = new $className;
                    $class->where($where)->save(['check_no' => $auditNo]);
                } catch (DbException $e) {
                    return error([], $e->getMessage());
                }
            }
            $model->commit();

            return success([], '操作成功');
        } catch (Exception $e) {
            $msg = $e->getMessage();
        } catch (DbException $e) {
            $msg = $e->getMessage();
        }
        return error([], $msg, 404);
    }

    public function save()
    {
        $post = $this->request->post();
        $user = $this->request->user;

        if (empty($post['order_id'])) return error([], "参数错误");
        if (empty($post['company_id'])) return error([], "请选择承办公司");
        if (empty($post['additional'])) return error([], "参数错误");

        // 获取订单信息
        $orderModel = new Order();
        $order = $orderModel->where('id', $post['order_id'])->find();
        if (empty($order)) return error([], "订单不存在");

        $model = new OrderAdditional();
        $model->startTrans();

        $source = [];
        $modelName = OrderAdditional::class;
        $additionals = $post['additional'];
        foreach ($additionals as $row) {
            try {
                $row['order_id'] = $post['order_id'];
                $row['user_id'] = $user['id'];
                $additionalModel = new OrderAdditional();
                if ($row['id']) {
                    $additional = $additionalModel->where('id', $row['id'])->find();
                    $additional->save($row);
                    $source[$modelName][] = $row['id'];
                } else {
                    $additionalModel->save($row);
                    $source[$modelName][] = $additionalModel->id;
                }
            } catch (DbException $e) {
                $model->rollback();
                return error([], $e->getMessage());
            }
        }

        $auditRsLogic = new AuditResultLogic();
        $master = $order->getData();
        $options = [
            'company_id' => $post['company_id'],
            'timing' => 'additional'
        ];
        try {
            $auditNo = $auditRsLogic->initAudit($master, $user, $source, $options);
            foreach ($source as $className=>$row) {
                try {
                    $where = [];
                    if (is_array($row)) {
                        $where[] = ['id', 'in', $row];
                    } else {
                        $where[] = ['id', '=', $row];
                    }
                    $class = new $className;
                    $class->where($where)->save(['check_no' => $auditNo]);
                } catch (DbException $e) {
                    return error([], $e->getMessage());
                }
            }
            $model->commit();

            return success([], '操作成功');
        } catch (Exception $e) {
            $msg = $e->getMessage();
        } catch (DbException $e) {
            $msg = $e->getMessage();
        }
        return error([], $msg, 404);
    }

    public function update()
    {
        $post = $this->request->post();
        $user = $this->request->user;

        $model = new OrderAdditional();
        $row = $model->where('id', $post['id'])->find();
        if (empty($row)) return error([], '数据已不存在');

        $model->startTrans();
        try {
            $post['user_id'] = $user['id'];
            $origin = $row->getData();
            $row->save($post);

            $changed = $row->getData();
            $modelName = OrderAdditional::class;
            OperateLog::write($user['id'], $post['order_id'], $origin, $changed, $modelName);
        } catch (DbException $e) {
            $model->rollback();
            return error([], $e->getMessage());
        }
        $model->commit();

        return success([], '更新成功');
    }
}